using System;
using System.Linq;
using GiveGraphModel;

namespace GiveGraphMappers
{
    public class OrganizationMapper : AbstractGiveGraphMapper<Organization>
    {
        #region Overrides of AbstractGiveGraphMapper<Organization>

        protected override int GetElementKey(Organization t)
        {
            return t.MemberId;
        }

        #endregion

        public Organization GetByEmail(string email)
        {
            return Call<Organization>((ctx) => (DoGetByEmail(ctx, email)));
        }

        protected Organization DoGetByEmail(GiveGraphContext ctx, string email)
        {
            return (from user in ctx.Organizations
                    where user.FacebookToken.Equals(email)
                    select user).SingleOrDefault();
        }

        public bool OrganizationExists(string email)
        {
            return Call<bool>((ctx) => (DoOrganizationExists(ctx, email)));
        }

        protected bool DoOrganizationExists(GiveGraphContext ctx, string email)
        {
            return (from user in ctx.Organizations
                    where user.FacebookToken.Equals(email)
                    select user).SingleOrDefault() != null;
        }
    }
}